//
//  ChildHoodUser.m
//  ChildHoodStemp
//
//  Created by xuzuotao on 13-8-24.
//
//

#import "ChildHoodUser.h"
#import "AppDelegate.h"

@interface ChildHoodUser()

-(NSString*)getUserDataPath;
-(NSString *)getMessagePath;
-(NSString *)getMemoriesPath;
-(NSString *)getNoticePath;
@end

@implementation ChildHoodUser


@synthesize userName=_userName;
@synthesize password=_password;
@synthesize relativeType = _relativeType;
@synthesize isChild = _isChild;
@synthesize userDataPath;
@synthesize memoriesPath;
@synthesize messagePath;
@synthesize roomsPath;
@synthesize resourcePath;
@synthesize myPostsPath;
@synthesize momentsPath;

-(id)init
{
    if(self = [super init])
    {
        _isChild = @"1";

    }
    return self;
}


+ (NSDictionary*) tableColumnsAndPropertiesMapWithoutPrimaryKey {
	return [NSDictionary dictionaryWithObjectsAndKeys:
			@"userId",	@"userId",            
			@"loginUserName",	@"loginUserName",
			@"password",	@"password",
			@"relativeType",@"relativeType",
			@"memoryId"    ,@"memoryId",
			@"isChild"    ,@"isChild",
			nil];
}

-(NSString *)getUserDataPathBefore{
//    NSString *address =  [[NSUserDefaults standardUserDefaults] objectForKey:KCURRENTLOGINSERVER];
    NSString *domain = @"uapi";
//    if(address != nil && [address length] > 0)
//    {
//        NSRange range = [address rangeOfString:@"."];
//        if(range.location != NSNotFound)
//        {
//            domain = [address substringToIndex:range.location];
//        }
//        if([domain isEqualToString:@"api"])
//        {
//            domain = @"";
//        }
//    }
    
    NSString *documentPath = [NSString stringWithFormat:@"%@/%@_%@_1.1.0%@/", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0], self.mName, self.relativeType, domain];
    return documentPath;
}

-(NSString*)getUserDataPath
{
   // NSString *documentPath = [[NSString alloc] initWithFormat:@"%@", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]];
  //  NSString* str = [NSString stringWithFormat:@"%@/messages_%@_%d.%@",documentPath, [ChildHoodService defaultService].childHoodUser.userName, [ChildHoodService defaultService].childHoodUser.relativeType,  MESSAGE_FILE];
   // [documentPath release];
//    NSString *address =  [[NSUserDefaults standardUserDefaults] objectForKey:KCURRENTLOGINSERVER];
    NSString *domain = @"uapi";
//    if(address != nil && [address length] > 0)
//    {
//        NSRange range = [address rangeOfString:@"."];
//        if(range.location != NSNotFound)
//        {
//            domain = [address substringToIndex:range.location];
//        }
//        if([domain isEqualToString:@"api"])
//        {
//            domain = @"";
//        }
//    }
      NSString *documentPath = [NSString stringWithFormat:@"%@/%@_%@_1.1.0%@/", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0], self.userName, self.relativeType,domain];
 //   return str;

    return documentPath;
}

-(NSString*)getRoomsPath
{
    // NSString *documentPath = [[NSString alloc] initWithFormat:@"%@", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]];
    //  NSString* str = [NSString stringWithFormat:@"%@/messages_%@_%d.%@",documentPath, [ChildHoodService defaultService].childHoodUser.userName, [ChildHoodService defaultService].childHoodUser.relativeType,  MESSAGE_FILE];
    // [documentPath release];
    
     NSString* str = [NSString stringWithFormat:@"%@rooms/", self.userDataPath];
   /* NSString *documentPath = [NSString stringWithFormat:@"%@/rooms/", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]];*/
    
    //   return str;
    
    return str;
}

-(NSString*)getResourcePath
{
    // NSString *documentPath = [[NSString alloc] initWithFormat:@"%@", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]];
    //  NSString* str = [NSString stringWithFormat:@"%@/messages_%@_%d.%@",documentPath, [ChildHoodService defaultService].childHoodUser.userName, [ChildHoodService defaultService].childHoodUser.relativeType,  MESSAGE_FILE];
    // [documentPath release];
    
    NSString *documentPath = [NSString stringWithFormat:@"%@/resource/", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]];
    //   return str;


    
    
    return documentPath;
}




-(void)createUserDataPath
{
    
//    NSMutableDictionary *acount = [AppDelegate loadAccountPlist];
//    
//    NSFileManager *manager = [NSFileManager defaultManager];
//
//    if ([[manager contentsOfDirectoryAtPath:[self getUserDataPathBefore] error:nil] count]>0/*&&[[acount objectForKey:@"Username"] isEqualToString:self.userName]&&[[acount objectForKey:@"MName"] isEqualToString:self.mName]*/) {
//        [manager moveItemAtPath:[self getUserDataPathBefore] toPath:[self getUserDataPath] error:nil];
//    }else{
////        [manager createDirectoryAtPath:self.userDataPath withIntermediateDirectories:NO attributes:nil error:nil];
////        [manager createDirectoryAtPath:self.memoriesPath withIntermediateDirectories:NO attributes:nil error:nil];
////        [manager createDirectoryAtPath:self.messagePath withIntermediateDirectories:NO attributes:nil error:nil];
////        [manager createDirectoryAtPath:self.roomsPath withIntermediateDirectories:NO attributes:nil error:nil];
////        [manager createDirectoryAtPath:self.resourcePath withIntermediateDirectories:NO attributes:nil error:nil];
////        [manager createDirectoryAtPath:self.noticePath withIntermediateDirectories:NO attributes:nil error:nil];
////        [manager createDirectoryAtPath:self.myPostsPath withIntermediateDirectories:NO attributes:nil error:nil];
////        [manager createDirectoryAtPath:self.momentsPath withIntermediateDirectories:NO attributes:nil error:nil];
//        [self createPath:manager path:self.userDataPath];
//        [self createPath:manager path:self.memoriesPath];
//        [self createPath:manager path:self.messagePath];
//        [self createPath:manager path:self.roomsPath];
//        [self createPath:manager path:self.resourcePath];
//        [self createPath:manager path:self.noticePath];
//        [self createPath:manager path:self.myPostsPath];
//        [self createPath:manager path:self.momentsPath];
//    }
    
    
//    [self test];
}

-(void)createPath:(NSFileManager *)fileManager path:(NSString *)path
{
    if(fileManager == nil || path == nil)
    {
        return;
    }
    BOOL isDir = NO;
    
    BOOL existed = [fileManager fileExistsAtPath:path isDirectory:&isDir];
    if ( !(isDir == YES && existed == YES) )
    {
        [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
    }

}


-(void)test
{

}

-(NSString *)getMessagePath
{
    NSString* str = [NSString stringWithFormat:@"%@message/", self.userDataPath];
    return str;
}

-(NSString *)getNoticePath
{
    NSString* str = [NSString stringWithFormat:@"%@notice/", self.userDataPath];
    return str;
}

-(NSString *)getMemoriesPath
{
    NSString* str = [NSString stringWithFormat:@"%@memories/", self.userDataPath];
    return str;
}

-(NSString*)getMemoryPortraitImagePath
{
    NSString *memoryPath = [self getMemoriesPath];
    NSString* str = [NSString stringWithFormat:@"%@portraitImage/", memoryPath];    
    return str;
}

-(NSString*)getMemoryPhotosPath
{
    NSString *memoryPath = [self getMemoriesPath];
    NSString* str = [NSString stringWithFormat:@"%@/photos/", memoryPath];
    return str;
}

-(NSString*)getMemoryPostPath
{
    NSString *memoryPath = [self getMemoriesPath];
    NSString* str = [NSString stringWithFormat:@"%@/post/", memoryPath];
    return str;
}

-(NSString*)getMemoryPairsPath
{
    NSString *memoryPath = [self getMemoriesPath];
    NSString* str = [NSString stringWithFormat:@"%@/pairs/", memoryPath];
    return str;
}

-(NSString*)getMyPostsPath
{
    NSString* str = [NSString stringWithFormat:@"%@posts/", self.userDataPath];
    return str;

    
}
-(NSString*)getMomentsPath
{
    NSString* str = [NSString stringWithFormat:@"%@posts/", self.userDataPath];
    return str;

    
}


@end
